<?php
namespace App\Goods\Services;

use App\Exceptions\GoodsException;
use App\Goods\Models\Goods;
use App\Goods\Models\GoodsBase;
use Exception;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;

class GoodsService
{

    protected function setData($data)
    {
        $save_data = [
            'sku_sn' => '',
            'spu_sn' => '',
            'common_name_id' => 0,
            'common_name' => '',
            'product_name' => '',
            'goods_name' => '',
            'trade_name' => '',
            'english_name' => '',
            'dosage' => '',
            'approve_date' => 0,
            'product_address' => '',
            'type_one' => 0,
            'type_two' => 0,
            'type_three' => 0,
            'type_four' => 0,
            'type_line' => '',
            'small_pack' => '',
            'pack_scale' => '',
            'big_pack' => '',
            'special_scale' => '',
            'special_pack' => '',
            'pinyin' => '',
            'title' => '',
            'subtitle' => '',
            'visual_image' => '',
            'cert_sn' => '',
            'end_date' => 0,
            'spec' => '',
            'note' => '',
            'info' => '',
        ];
        foreach($data as $k => $v){
            if(!isset($save_data[$k])){
                continue;
            }
            $save_data[$k] = $v;
        }
        $save_data['end_date'] = strtotime($save_data['end_date']);
        $save_data['approve_date'] = strtotime($save_data['approve_date']);
        return $save_data;
    }

    /**
     * 添加商品
     * @param $data
     * @return
     * @throws GoodsException
     */
    public function save($data)
    {
        // 查询商品基本信息
        $goods_base = GoodsBase::find($data['base_id']);
        if(!$goods_base){
            throw new GoodsException('商品基本信息不存在或已删除！');
        }
        $data['common_name_id'] = $goods_base->common_name_id;
        $data['common_name'] = $goods_base->common_name;
        // 分类处理
        $type_line = [
            $data['type_one'],
            $data['type_two'],
            $data['type_three'],
            $data['type_four'],
        ];
        $data['type_line'] = implode('-', $type_line);
        $data['spu_sn'] = $goods_base->spu_sn;
        $data['sku_sn'] = $goods_base->sku_sn;
        $save_data = $this->setData($data);
        DB::beginTransaction();
        try{
            // 添加数据
            if(!$goods = Goods::create($save_data)){
                throw new Exception('添加失败！');
            }
            // 添加属性
            $attr = json_decode($data['attr']);
            $attr_arr = [];
            // 查询商品基本信息表的值，用做数据冗余
            $base_attr = $goods_base->hasAttr;
            $base_attr_val = [];
            foreach($base_attr as $v){
                $base_attr_val[$v->pivot->attr_id] = $v->pivot->value;
            }
            foreach($attr as $v){
                $attr_arr[$v->id] = [
                    'value' => $v->value,
                    'base_value' => $base_attr_val[$v->id]
                ];
            }
            $res = $goods->hasAttr()->sync($attr_arr);
            if(!$res){
                throw new Exception('添加属性失败！');
            }
            // 添加图片
            $images = json_decode($data['images']);
            $save_images = [];
            foreach($images as $k => $v){
                $save_images[$k]['goods_id'] = $goods->id;
                $save_images[$k]['image'] = $v->url;
            }
            $res = $goods->hasImages()->insert($save_images);
            if(!$res){
                throw new Exception('添加图片失败！');
            }
            DB::commit();
            return $goods;
        }catch (Exception $exception){
            DB::rollBack();
            throw new GoodsException('添加失败！');
        }

    }
}
